using System;
using System.Collections.Generic;
using System.Text;
using FrameworkX.ObjectSpace.Data.SqlObjects;
using FrameworkX.Application.Security.Objects;

namespace FrameworkX.TinyCms.Core.Security
{
    internal static class DataAccessHelper
    {
        internal static WhereClause GetUserByLogin(string login)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(
                SqlExpression.Field("Login"), SqlExpression.Constant(SqlConstant.String(login)),
                CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause DeleteBySessionId(long SessionId)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(
                SqlExpression.Field("SessionID"), SqlExpression.Constant(SqlConstant.Number( SessionId ) ),
                CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause GetUserGroupMapByUserIDGroupID(long userID, long groupID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            
            // UserID
            wc.Items.Add( WhereItem.CreateCompare( SqlExpression.Field( UserGroupMap.PROPERTY_USERID ),
                SqlExpression.Constant( SqlConstant.Number(userID)),
                CompareOperator.Equal ));

            // GroupId
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field(UserGroupMap.PROPERTY_GROUPID),
                SqlExpression.Constant(SqlConstant.Number(groupID)),
                CompareOperator.Equal));

            return wc;
        }

        internal static WhereClause GetSession(string SessionKey)
        {
            WhereClause wc = new WhereClause( WhereClauseRelationship.And );
            wc.Items.Add( WhereItem.CreateCompare(
                SqlExpression.Field( "SessionKey" ), SqlExpression.Constant( SqlConstant.String( SessionKey ) ),
                CompareOperator.Equal ) );
            return wc;
        }

        internal static WhereClause GetUserGroupMapByUserID(long userID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);

            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field(UserGroupMap.PROPERTY_USERID),
                SqlExpression.Constant(SqlConstant.Number(userID)),
                CompareOperator.Equal));

            return wc;
        }

        internal static WhereClause GetUserGroupMapByGroupID(long groupID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field(UserGroupMap.PROPERTY_GROUPID),
                SqlExpression.Constant(SqlConstant.Number(groupID)),
                CompareOperator.Equal));

            return wc;
        }

        internal static WhereClause GetGroupAccessRightMapByGroupID(int groupID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field(GroupAccessRightMap.PROPERTY_GROUPID),
                SqlExpression.Constant(SqlConstant.Number(groupID)),
                CompareOperator.Equal));

            return wc;
        }

        internal static WhereClause RemoveAccessRightsByGroup(long[] accessRights, long groupID)
        {            
            WhereClause wc = new WhereClause(WhereClauseRelationship.Or);
            foreach (int rightID in accessRights)
            {
                WhereClause wc2 = new WhereClause(WhereClauseRelationship.And);
                wc2.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("AccessRightID"),
                                SqlExpression.Constant(SqlConstant.Number(rightID)),
                                CompareOperator.Equal));
                wc2.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("GroupID"),
                                SqlExpression.Constant(SqlConstant.Number(groupID)),
                                CompareOperator.Equal));
                wc.SubClauses.Add(wc2);
            }
            return wc;
        }


        internal static WhereClause RemoveAccessRightsByGroup( long groupID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.Or);
                wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("GroupID"),
                                SqlExpression.Constant(SqlConstant.Number(groupID)),
                                CompareOperator.Equal));
            return wc;
        }


        internal static WhereClause GetGroupAccessRightMap(long groupID, long accessRightID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("AccessRightID"),
                            SqlExpression.Constant(SqlConstant.Number(accessRightID)),
                            CompareOperator.Equal));
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("GroupID"),
                            SqlExpression.Constant(SqlConstant.Number(groupID)),
                            CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause RemoveAllUsersFromGroup(long groupID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("GroupID"),
                            SqlExpression.Constant(SqlConstant.Number(groupID)),
                            CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause IsUserLoggedIn(long userID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("IsActive"),
                            SqlExpression.Constant(SqlConstant.Number(1)),
                            CompareOperator.Equal));

            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("UserID"),
                            SqlExpression.Constant(SqlConstant.Number(userID)),
                            CompareOperator.Equal));
            return wc;
        }



        internal static WhereClause GetActiveUsers()
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("IsActive"),
                            SqlExpression.Constant(SqlConstant.Number(1)),
                            CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause GetUserSessions(long userID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("UserID"),
                            SqlExpression.Constant(SqlConstant.Number(userID)),
                            CompareOperator.Equal));
            return wc;
        }

        internal static WhereClause GetTodoTasks(long userID)
        {
            WhereClause wc = new WhereClause(WhereClauseRelationship.And);
            wc.Items.Add(WhereItem.CreateCompare(SqlExpression.Field("UserID"),
                            SqlExpression.Constant(SqlConstant.Number(userID)),
                            CompareOperator.Equal));
            return wc;            
        }
    }
}
